<template>
  <!-- 首页商品列表 -->
  <div>
    <ul class="item_list">
      <li v-for="(childItem, childIndex) in item" :key="childIndex" class="item_box">
        <router-link to="/detail" tag="div" class="item_img" @click.native="getGoodDetail(childItem.goods_id)">
          <img :src="childItem.goods_imgs[0]">
        </router-link>
        <h3 class="item_name">{{ childItem.goods_name }}</h3>
        <p class="item_info"> {{ childItem.goods_introduce }}</p>
        <div class="item_label_box">
          <div :class="childItem.goods_label === '' ? '' : 'item_label'">{{ childItem.goods_label }}</div>
        </div>
        <div class="item_price">
          <span class="item_current">¥{{ childItem.goods_price }}</span>
          <span class="item_oldPrice">{{ childItem.goods_oldprice | priceFilter }}</span>
          <add-cart :good="childItem" :id="childItem.goods_id"></add-cart>
        </div>
      </li>
    </ul>
  </div>
</template>

<script>
import AddCart from '@/pages/common/AddCart.vue'
export default {
  name: 'HomeGoods',
  props: ['item'],
  filters: {
    priceFilter (value) {
      if (value !== 0) return '¥' + value
      return ''
    }
  },
  methods: {
    getGoodDetail (id) {
      this.$store.commit('getGoodDetail', id)
    }
  },
  components: {
    AddCart
  }
}
</script>

<style lang="less" scoped>
// @import '../../../assets/styles/goods.css';
.item_list {
  display: flex;
  flex-wrap: wrap;
  .item_box {
    flex: 48%;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    height: 10.8rem;
    padding: .4rem;
    background-color: #fff;
    margin-bottom: .4rem;
    border-radius: .4rem;
    .item_img {
      align-self: center;
      width: 5rem;
      height: 5rem;
      margin: .2rem 0;
      img {
        width: 100%;
      }
    }
    .item_name {
      font-size: .64rem;
      color: #333;
      /* 强制文字换行 */
      // white-space: pre-wrap;
      // word-wrap: break-word;
    }
    .item_info {
      overflow: hidden;
      font-size: .56rem;
      color: #666;
      white-space: nowrap;
      text-overflow: ellipsis;
      margin: .3rem 0;
    }
    .item_label_box {
      flex: 1;
      .item_label {
        height: .64rem;
        line-height: .64rem;
        font-size: .48rem;
        color: red;
        border: 0.5px solid red;
        border-radius: .32rem;
        padding: 0 .12rem;
      }
    }
    .item_price {
      position: relative;
      height: 1rem;
      line-height: 1rem;
      margin-top: .2rem;
      .item_current {
        font-size: .64rem;
        color: rgb(255, 20, 147);
      }
      .item_oldPrice {
        font-size: .48rem;
        text-decoration: line-through;
        color: #999;
        padding-left: .4rem;
      }
    }
  }
  .item_box:nth-child(odd) {
    margin-right: 3%;
  }
}
</style>
